/**
 * AJAX Object
 * 
 * @author Javier Pérez <javier@javierperez.eu>
 * @license GPL
 * @version 1.0
 */
var RF_AJAX = {
	xmlhttprequest: new XMLHttpRequest(),
	onload: function(a) {},
	onerror: function(code,e) {},
	url: null,
	params: null,
	xmlhttpchange: function() {
 		if (RF_AJAX.xmlhttprequest.readyState == 4) {
			if (RF_AJAX.xmlhttprequest.status == 200) {
				RF_AJAX.onload(RF_AJAX.xmlhttprequest);
			} else {
				RF_AJAX.onerror(RF_AJAX.xmlhttprequest.status, RF_AJAX.xmlhttprequest)
			}
 		}
	},
	request: function(data) {
		RF_AJAX.onload = data.onload ? data.onload : function(a) {
			if (RF_AJAX.onloadtext) {
	 			RF_AJAX.onloadtext.ajaxfunction(RF_DOM.create({
					tag: 'div',
					inner: a.responseText
				}));
			}
		}; 
		RF_AJAX.onloadtext = data.onloadtext ? data.onloadtext : function() {}; 
		RF_AJAX.onerror = data.onerror ? data.onerror : function() {}; 
		RF_AJAX.url = data.url ? data.url : null; 
		RF_AJAX.params = data.params ? data.params : null; 
		RF_AJAX.method = data.method ? data.method.toLowerCase() : 'get';
		
		RF_AJAX.xmlhttprequest.onreadystatechange=RF_AJAX.xmlhttpchange;
		RF_AJAX.xmlhttprequest.open(RF_AJAX.method, RF_AJAX.url, true);
		RF_AJAX.xmlhttprequest.send(RF_AJAX.method == 'post' ? RF_AJAX.method : null);
	}
}

/**
 * DOM Object
 * Funciones para el manejo del DOM
 * 
 * @author Javier Pérez <javier@javierperez.eu>
 * @license GPL
 * @version 1.0
 */
var RF_DOM = {
	doc: function() {
		return window._content.document;
	},
	
	create: function(data) {
		var o = RF_DOM.doc().createElement(data.tag);
		if (data.inner) o.innerHTML = data.inner;
		if (data.style) o.setAttribute('style',data.style);
		if (data.classname) o.className = data.classname;
		if (data.src) o.src = data.src;
		if (data.border) o.border = data.border;
		if (data.href) o.href = data.href;
		return o;
	},
	
	classeds: function(o,t,c) {
		// Elementos encontrados
		var r = [];
		if (o && o.getElementsByTagName) {
			// Lista de elementos con tag <t>
			var l = o.getElementsByTagName(t);
			// Busqueda de elementos con clase <c>
			for (var i=0; i<l.length; i++) {
				if (l[i].className==c) {
					r.push(l[i]);
				}
			}
		}
		// Resultado
		return r;
	},
	
	redomain: function(a, doc, domain) {
		if (a.href) a = a.href;
		var _url = RF_STRING.replace(a, doc.location.host, domain);
		_url = RF_STRING.replace(_url,'https://','http://');
		return _url;
	}
}
function $(id) {
	//if (typeof(id) != 'String')
	//	return id;
	return window.document.getElementById(id);
}

/**
 * STRING Object
 * 
 * @author Javier Pérez <javier@javierperez.eu>
 * @license GPL
 * @version 1.0
 */
var RF_STRING = {
	replace: function(s,r,w) {
		if (!s) return s;
		return s.split(r).join(w);
	},
	
	trim: function(string) {
		if (!string) return '';
		var newString  = '';
		var substring  = '';
		beginningFound = false;
		// copy characters over to a new string
		// retain whitespace characters if they are between other characters
		for (var i = 0; i < string.length; i++) {
		       // copy non-whitespace characters
		       if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
		               // if the temporary string contains some whitespace characters,copy them first
		               if (substring != '') {
		                       newString += substring;
		                       substring = '';
		               }
		               newString += string.charAt(i);
		               if (beginningFound == false) beginningFound = true;
		       }
		       // hold whitespace characters in a temporary string if they follow a non-whitespace character
		       else if (beginningFound == true) substring += string.charAt(i);
		}
		return newString;
	}
} 

/**
 * MATH Object
 * 
 * @author Javier Pérez <javier@javierperez.eu>
 * @license GPL
 * @version 1.0
 */
var RF_MATH = {
	
	/**
	 * random: Genera un número aleatorio
	 * @param Integer n Máximo
	 */
	random: function(n) {
		return Math.round(Math.random()*n);
	}
}